// import { WindowPool } from './window-pool'
import { BrowserWindow } from 'electron'
import { app } from 'electron'
import { load } from './window-load'
import path from 'path'

class WindowUtil {
  // private pool: WindowPool | null = null
  wins: Record<string, BrowserWindow> = {}

  constructor() {
    //
  }
  init(): void {
    // this.pool = new WindowPool(1)
  }

  addClosedEvent(win: BrowserWindow, key: string): void {
    win.on('closed', () => {
      delete this.wins[key]

      if (Object.keys(this.wins).length === 0) {
        app.quit()
      }
    })
  }

  mainWinCloseEvent(win: BrowserWindow): void {
    win.on('close', (e) => {
      console.log('被触发了', e)
      e.preventDefault()
      win.hide()
    })
  }

  createMainWindow(): BrowserWindow {
    const win = new BrowserWindow({
      width: 900,
      height: 900,
      title: '主页面',
      show: false,
      webPreferences: {
        nodeIntegration: true,
        nodeIntegrationInWorker: true,
        sandbox: false,
        contextIsolation: false
      }
    })

    load(win)

    this.wins['main'] = win

    win.on('ready-to-show', () => {
      win.show()
    })

    this.addClosedEvent(win, 'main')
    this.mainWinCloseEvent(win)

    return win
  }

  createUpdateWidow(): BrowserWindow {
    const win = new BrowserWindow({
      width: 1000,
      height: 800,
      transparent: true,
      backgroundColor: '#00000000',
      frame: false,
      resizable: false,
      maximizable: false,
      minimizable: false,
      show: false,
      webPreferences: {
        sandbox: true,
        preload: path.join(__dirname, '../preload/index.js')
      }
    })

    load(win, {
      hash: '#/update'
    })

    win.on('ready-to-show', () => {
      win.show()
    })

    return win
  }
}

export default new WindowUtil()
